Requirements

For this excercise you need a (free) account at http://www.realtime.co/; if you create an account and start a "Realtime Messaging Free" subscription, you can put its "Application Key" in the variable below. This key will then be used in the communications we'll set up further on.

You will also have to have your Raspberry Pi connected to the internet for the communications to work.


In [ ]:
APPKEY = "******"

We will again use the "RPi.GPIO" library in our code
And for the GPIO pins we will, as usual, use the BCM numbering (cfr numbers on the case)


In [ ]:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

ortc is a Python library that is linked to the realtime.co cloud communication system (http://www.realtime.co/)

You can compare it to e-mail, Twitter or Facebook Messenger, but intended to be used by computers or devices.


In [ ]:
import ortc
oc = ortc.OrtcClient()
oc.cluster_url = "http://ortc-developers.realtime.co/server/2.1"

# Connecting to the account
oc.connect(APPKEY)

With person_pins we create a dictionary that links each of the GPIO pins to the person who the door bell of that pin belongs to.


In [ ]:
person_pins = {
    17: "Maleficent",
    27: "BigBadWolf",
}

Let's see if that works:


In [ ]:
print(person_pins[17])

Now let's create a function with a parameter called "pin" (no confusion possible there...); we intend to use this function to send a message, specific to the relevant pin.
If the value of pin is 17, we'll send a message "Maleficent" on the "doorbell" channel.


In [ ]:
def send_message(pin):
    oc.send("deurbel", person_pins[pin])

Last piece of the setup: for each of the possible pin values, we 'll:

  1. set the pin as input (to listen for button presses)
  2. register an event detection (if the pin goes from 1 to 0 -GPIO.FALLING-, send_message is executed)

In [ ]:
for pin in person_pins:
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
    GPIO.add_event_detect(pin, GPIO.FALLING, send_message, 200)

Time to test is out: press the button and see if your message appears in the cloud!

Follow the following procedure to check it:

  • log in on the Realtime website
  • click on your name to bring op your account
  • click on Subscriptions (the shopping cart in the left upper corner)
  • open the "Realtime Messaging Free" subscription and click "Console"
  • Input "doorbell" as Channel and click [Subscribe]
  • now you can press a button on you Raspberry Pi and you'll see the message appear

And clean up again:


In [ ]:
for pin in person_pins:
    GPIO.remove_event_detect(pin)

In [ ]:
GPIO.cleanup()